package com.aiton.bamin.bamindaijiadrier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aiton.administrator.shane_library.shane.utils.IsMobileNOorPassword;
import com.aiton.bamin.bamindaijiadrier.R;
import com.aiton.bamin.bamindaijiadrier.constant.Constant;


public class ChangeNameActivity extends ZjbBaseActivity implements View.OnClickListener {

    private EditText mEdt_Name;
    private String name;
    private String mType;
    private String info;
    private TextView mTv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_name);
        init();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {

        switch (mType) {
            case Constant.TYPE.CHANGE_NAME:
                initInfo("请填写姓名");
                mTv_title.setText("姓名");
                mEdt_Name.setHint("姓名");
                break;
            case Constant.TYPE.EMERGENCY:
                initInfo("请填写紧急联系人");
                mTv_title.setText("紧急联系人");
                mEdt_Name.setHint("紧急联系人");

                break;
        }
    }

    private void initInfo(String str) {
        if (info != null) {
            if (info.equals(str)) {
                info = "";
            }
            mEdt_Name.setText(info);
        }
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);
        findViewById(R.id.tv_complete).setOnClickListener(this);
    }

    @Override
    protected void initSP() {
        Intent intent = getIntent();
        name = intent.getStringExtra(Constant.INTENT_KEY.CHANGE_NAME);

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        mType = intent.getStringExtra(Constant.INTENT_KEY.ADD_INFO_TYPE);
        switch (mType) {
            case Constant.TYPE.CHANGE_NAME:
                info = intent.getStringExtra(Constant.INTENT_KEY.CHANGE_NAME);
                break;
            case Constant.TYPE.EMERGENCY:
                info = intent.getStringExtra(Constant.INTENT_KEY.EMERGENCY);
                break;
        }
    }

    @Override
    protected void findID() {
        mEdt_Name = (EditText) findViewById(R.id.edt_name);
        mTv_title = (TextView) findViewById(R.id.tv_title);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;
            case R.id.tv_complete:
                completeType();
                break;
        }
    }

    /**
     * 完成
     */
    private void completeType() {
        Intent intent = new Intent();
        String info = mEdt_Name.getText().toString().trim();
        switch (mType) {
            case Constant.TYPE.CHANGE_NAME:
                if (IsMobileNOorPassword.checkChineseName(info)) {
                    intent.putExtra(Constant.INTENT_KEY.CHANGE_NAME, info);
                    setResult(Constant.REQUEST_RESULT_CODE.CHANGE_NAEM, intent);
                    finishTo();
                } else {
                    mEdt_Name.setError("请输入正确的姓名");
                }
                break;
            case Constant.TYPE.EMERGENCY:
                if (IsMobileNOorPassword.isMobileNO(info)) {
                    intent.putExtra(Constant.INTENT_KEY.EMERGENCY, info);
                    setResult(Constant.REQUEST_RESULT_CODE.EMERGENCY, intent);
                    finishTo();
                } else {
                    mEdt_Name.setError("请输入正确的手机号码");
                }
                break;

        }
    }
}
